﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LiuYan.Service;
using LiuYan.Service;
using LiuYan.Web.Areas.Admin.Controllers;
using LiuYan.Web.Filters;
using LiuYan.Base;

namespace LiuYan.Web.Areas.Admin.Controllers
{
    public class ManageController : BaseController
    {
       
     //[SessionFilter]
        public ActionResult Index()
        {
            ViewData["pmenu"] = new MenuService().GetPmenu(UserInfo().RoleId);
            ViewData["roleId"] = UserInfo().RoleId;
            ViewData["name"] = Server.UrlDecode(UserInfo().Name);
            return View();
        }
       
        public ActionResult Home()
        {
            ViewData["order"] = new OrderService().GetOrder1(2, 0, 10, "");
            return View();
        }

       

        /// <summary>
        /// 获取登陆验证码
        /// </summary>
        /// <returns></returns>
        public ActionResult ValideteImg()
        {
            VerificationCode verificationCode = new VerificationCode();
            string rnd = string.Empty; //验证码
            Bitmap map = verificationCode.DrawImage(out rnd);
            Response.Cookies.Add(new HttpCookie("CheckCode", rnd)); //将验证码放入cookie
            MemoryStream ms = new MemoryStream();
            map.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return File(ms.ToArray(), "image/gif");
        }

        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        ///  /// <param name="yzm">验证码</param>
        /// <returns></returns>
        public string UserLogin(string username, string pwd, string yzm)
        {
            try
            {
                string checkCode = Request.Cookies["CheckCode"].Value; //获取验证码
                Response.Cookies["CheckCode"].Expires = DateTime.Now; //设置cookie过期时间
                string s = checkCode.ToUpper();
                if (checkCode.ToLower() != yzm.ToLower() || checkCode.ToUpper() != yzm.ToUpper())
                {
                    return "errorcode";
                }
                DataTable table = new LoginService().Login(username, pwd);
                if (table.Rows.Count != 0)
                {
                    Session["admin"] = table.Rows[0]["UserName"];
                }
                else
                {
                    return "fail";
                }
                return "success";

            }
            catch (Exception e)
            {

            }
            return "";
        }
         //[SessionFilter]
        public ActionResult EditPwd()
        {
            return View();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="password"></param>
        /// <param name="newpwd"></param>
        /// <returns></returns>
         //[SessionFilter]
        public int EditPassword(string password, string newpwd)
        {
            return new LoginService().EditPwd(password, newpwd,UserInfo().Id);
        }
         //[SessionFilter]
        public void Exit()
         {
             Session["admin"] = null;
             HttpCookie cookies = new HttpCookie("user");
             cookies.Expires = DateTime.Now.AddDays(-1);
             Response.Cookies.Add(cookies);
             Response.Redirect(Url.Action("Index","Login"));
         }
        public DataTable GetCmenu(int pId, int roleId)
        {
            return new MenuService().GetCmenu(roleId, pId);

        }
        public DataTable GetCmenu_(int roleId)
        {
            return new MenuService().GetCmenu_(roleId);
        }

         
    }
}
